package com.ljt.controller;


import com.ljt.pojo.User;
import com.ljt.service.UserService;
import com.ljt.util.Result;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;


@CrossOrigin(allowCredentials = "true",allowedHeaders = "*")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @ResponseBody
    @PostMapping("/doLogin")
    public Object doLogin(@RequestParam(value="username",required = false) String username,
                          @RequestParam(value="password",required = false) String password) {
        //System.out.println("## running"+username+password);
        //查询数据库
        User user = userService.login(username, password);
       // System.out.println("## ## "+user.getUsername()+user.getPassword());
        JSONObject json = JSONObject.fromObject(user);
        if(user==null){
            return Result.fail("账号或密码错误");
        }
        return Result.success(json);
    }

    @ResponseBody
    @GetMapping("/getUser")
    public List<User> getUser(){
        List<User> userList = userService.select();
        return userList;
    }

    @ResponseBody
    @GetMapping("/getUserByUsername/{username}")
    public User getUser(@PathVariable("username") String username){
        User user = userService.selectByUsername(username);
        return user;
    }

}
